Tensorflow


In [ ]:
import matplotlib
import matplotlib.pyplot as plt
import tensorflow as tf
import time

def get_times(maximum_time):

    device_times = {
        "/gpu:0":[],
        "/cpu:0":[]
    }
    matrix_sizes = list(range(500,50000,50))

    for size in matrix_sizes:
        for device_name in list(device_times.keys()):

            print(("####### Calculating on the " + device_name + " #######"))

            shape = (size,size)
            data_type = tf.float16
            with tf.device(device_name):
                r1 = tf.random_uniform(shape=shape, minval=0, maxval=1, dtype=data_type)
                r2 = tf.random_uniform(shape=shape, minval=0, maxval=1, dtype=data_type)
                dot_operation = tf.matmul(r2, r1)


            with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
                    start_time = time.time()
                    result = session.run(dot_operation)
                    time_taken = time.time() - start_time
                    #print(result)
                    device_times[device_name].append(time_taken)

            print(device_times)

            if time_taken > maximum_time:
                return device_times, matrix_sizes


device_times, matrix_sizes = get_times(1.5)
gpu_times = device_times["/gpu:0"]
cpu_times = device_times["/cpu:0"]

plt.plot(matrix_sizes[:len(gpu_times)], gpu_times, 'o-', label='gpu')
plt.plot(matrix_sizes[:len(cpu_times)], cpu_times, 'o-', label='cpu')
plt.ylabel('Time')
plt.xlabel('Matrix size')
plt.legend(loc='best')
plt.show()

In [ ]:


In [ ]:


In [ ]:
import torch

In [ ]:
x = torch.Tensor(5,3)

In [ ]:
print(x)

In [ ]:
x = torch.rand(5,3)

In [ ]:
y = torch.rand(5,3)

In [ ]:
x+y

In [ ]:
print((torch.add(x,y)))

In [ ]:
result = torch.Tensor(5,3)

In [ ]:
torch.add(x,y, out=result)

In [ ]:
x

In [ ]:
x[:,1]

In [ ]:
a = torch.ones(5)

In [ ]:
a.add_(1)

In [ ]:
b = a.numpy()
print(b)

In [ ]:
b

In [ ]:
c = np.ones(5)

In [ ]:
d = torch.from_numpy(c)

In [ ]:
np.add(c, 1, out=c)

In [ ]:
if torch.cuda.is_available():
    x = x.cuda()
    y = y.cuda()
    x+y

In [ ]:
from torch.autograd import Variable

In [ ]:
x = Variable(torch.ones(2,2), requires_grad=True)

In [ ]:
x

In [ ]:
y = x + 2

In [ ]:
y

In [ ]:
print((y.grad_fn))

In [ ]:
z = y*y*3

In [ ]:
out = z.mean()

In [ ]:
print((z.out))

In [ ]:
out

In [ ]:
out.backward()

In [ ]:
print((x.grad))

In [ ]:
x

In [ ]:
x.data

In [ ]:
x.grad

In [ ]:
type(x.grad_fn)

In [ ]:
Variable?

Theano


In [ ]:
import theano

In [ ]: